/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.dao;

import com.bookshop.entity.Book;
import com.bookshop.entity.Customer;
import com.bookshop.entity.Customerorder;
import com.bookshop.entity.CustomerorderBook;
import com.bookshop.entity.CustomerorderBookPK;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Singleton;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Samir
 */
@Singleton(name = "com.bookshop.dao.CustomerorderBook")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class DaoCustomerOrderBookImpl implements IDaoCustomerOrderBookLocal, IDaoCustomerOrderBookRemote, Serializable {

    @PersistenceContext
    private EntityManager em;
    
    private Customerorder co = new Customerorder();
    private CustomerorderBook cob1 = new CustomerorderBook();
    private CustomerorderBookPK coPk = new CustomerorderBookPK();
    private List<CustomerorderBook> listCob = new ArrayList<CustomerorderBook>();
    private boolean retour = true;

    @Override
    public boolean addItem(Book book, int quantity) {
        //book = em.find(Book.class, cob1.getBook().getId());
        co.setAmount(co.getAmount()+ book.getPrice());
        co.setCustomerid(em.find(Customer.class, 1));
        em.persist(co);
        cob1.setBook(book);
        cob1.setQuantity(quantity);
        cob1.setCustomerorder(co);
        em.persist(cob1);
        listCob.add(cob1);
        for (CustomerorderBook cob2 : listCob) {
            cob2.setQuantity(cob1.getQuantity());
            cob2.setBook(cob1.getBook());
            cob2.setCustomerorder(cob1.getCustomerorder());
//            coPk.setBookid(book.getId());
//            coPk.setCustomerOrderid(1);
            //cob2.setCustomerorderBookPK(coPk);
            em.persist(cob2);
            System.out.println("ajout_ok");
        }
        return retour;

    }

    @Override
    public String removeItem(CustomerorderBook item) {
        em.remove(item);
        return "myCart.xhtml";
    }

    /**
     * retourne le nombre d'article dans le panier
     *
     * @return
     */
    @Override
    public int getItemNumberFromCart() {
        Customerorder customerOrder = null;
        int nbArticle = 0;
        List<Integer> liste = em.createNamedQuery("CustomerorderBook.itemNumber").setParameter("customerOrderid", customerOrder.getCustomerid()).getResultList();
        if (liste.isEmpty()) {
            return nbArticle;
        } else {
            for (int i : liste) {
                nbArticle = i;
                return nbArticle;
            }
        }
        return nbArticle;
    }

    @Override
    public List<CustomerorderBook> getCartItems(CustomerorderBookPK customerOrderBookPk) {
        List<CustomerorderBook> items = em.createNamedQuery("CustomerorderBook.items").setParameter("customerOrderid", customerOrderBookPk.getCustomerOrderid()).getResultList();
        return items;
    }
//    public void empty() {
//        cartItems.clear();
//    }
}
